package org.unicat.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.unicat.mapper.StudentMapper;
import org.unicat.mapper.TeacherMapper;
import org.unicat.mapper.UserInfoMapper;
import org.unicat.mapper.UserMapper;
import org.unicat.model.Student;
import org.unicat.model.Teacher;
import org.unicat.model.User;
import org.unicat.model.UserInfo;
import org.unicat.service.UserRegistService;

@Service
public class UserRegistServiceImpl implements UserRegistService {

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private UserInfoMapper userInfoMapper;

    @Autowired
    private StudentMapper studentMapper;

    @Autowired
    private TeacherMapper teacherMapper;

    @Override
    public void UserRegist(UserInfo userInfo) {
        //先插入user表（登录信息）
        User user = new User();
        user.setId(userInfo.getId());
        user.setUsername(userInfo.getUsername());
        user.setPassword(userInfo.getPassword());
        userMapper.addUser(user);

        //再加入userInfo表（用户个人信息）
        //通过注册的都是学生
        //老师用户只有管理员才能添加
        userInfo.setIdentity("student");
//        System.out.println("学生注册:" + userInfo);
        userInfoMapper.addUserInfo(userInfo);

        //注册的是学生，加入student表
        Student student = new Student();
        student.setName(userInfo.getUsername());
        student.setMajor(userInfo.getMajor());
        studentMapper.addStudent(student);
    }

    @Override
    public void teacherRegist(UserInfo userInfo) {
        //先插入user表（登录信息）
        User user = new User();
        user.setId(userInfo.getId());
        user.setUsername(userInfo.getUsername());
        user.setPassword(userInfo.getPassword());
        userMapper.addUser(user);

        //再加入userInfo表（用户个人信息）
        userInfo.setIdentity("teacher");
//        System.out.println("老师注册:" + userInfo);
        userInfoMapper.addUserInfo(userInfo);

        //注册老师，加入teacher表
        Teacher teacher = new Teacher();
        teacher.setName(userInfo.getUsername());
        teacher.setMajor(userInfo.getMajor());
        teacherMapper.addTeacher(teacher);
    }

    @Override
    public void studentRegist(UserInfo userInfo) {
        //先插入user表（登录信息）
        User user = new User();
        user.setId(userInfo.getId());
        user.setUsername(userInfo.getUsername());
        user.setPassword(userInfo.getPassword());
        userMapper.addUser(user);

        //再加入userInfo表（用户个人信息）
        userInfo.setIdentity("student");
        //System.out.println("学生注册:" + userInfo);
        userInfoMapper.addUserInfo(userInfo);

        //注册学生，加入student表
        Student student = new Student();
        student.setName(userInfo.getUsername());
        student.setMajor(userInfo.getMajor());
        studentMapper.addStudent(student);
    }
}
